//
//  Global.m
//  HJBIPAD
//
//  Created by LiYinDi on 14-11-16.
//  Copyright (c) 2014年 RuiBen. All rights reserved.
//

#import "Global.h"
static NSMutableString *tokenStr;
static NSMutableString *scanResultStr;
static NSMutableString *accountCodeStr;
static NSMutableString *accountNameStr;
static NSMutableString *rongYunTokenStr;

static NSMutableString *selectedGoodTypeStr;
static MBProgressHUD *_textHUD;
static MBProgressHUD *HUD;
static NSMutableString *phoneNumStr;
@implementation Global
+ (NSDate *)dateByAddingComponents:(NSDate *)selectedDate
                               day:(NSInteger)days
{
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    offsetComponents.day = days;
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *endOfWorldWar3 = [gregorian dateByAddingComponents: offsetComponents
                                                        toDate: selectedDate
                                                       options: 0];
    
    return endOfWorldWar3;
}
+ (NSString *)stringFromChangeDate:(NSDate *)date{
    
    NSDate *resultDate;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
     NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];
     formatter.timeZone = sourceTimeZone;
     resultDate = [formatter dateFromString:[NSString stringWithFormat:@"%@",date]];
     NSString *destDateString = [formatter stringFromDate:date];
     
     return destDateString;
}
+ (NSString *)theStartOfMonth:(NSDate *)selectedDate
{
    NSDate *beginOfMonth = nil;
    NSTimeInterval endDate;
    NSDate *today =selectedDate;
    // [NSDate date];
    NSCalendar *gregrioan = [NSCalendar autoupdatingCurrentCalendar];
    BOOL success = [gregrioan rangeOfUnit:NSMonthCalendarUnit startDate:&beginOfMonth interval:&endDate forDate:today];
    
    if (!success)
        NSLog(@"Error:failed to get day.");
   
    int day = ((int)endDate)/(3600*24);
    
    NSDateComponents *monthComponent = [gregrioan components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:today];
    
    NSDateComponents *component = [[NSDateComponents alloc]init];
    [component setDay:day];
    [component setMonth:[monthComponent month]];
    [component setYear:[monthComponent year]];
   
   
   // NSDate *lastDate = [gregrioan dateFromComponents:component];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
  
    //NSString *strLastDate = [formatter stringFromDate:lastDate];
    
    //NSLog(@"strlastdate====%@",strLastDate);
  
    NSString *strFirstDate = [formatter stringFromDate:beginOfMonth];
    
   // NSLog(@"strFirstDate====%@",strFirstDate);
    return strFirstDate;

}
+ (NSString *)theEndOfMonth:(NSDate *)selectedDate
{
    NSDate *beginOfMonth = nil;
    NSTimeInterval endDate;
    NSDate *today =selectedDate;
    // [NSDate date];
    NSCalendar *gregrioan = [NSCalendar autoupdatingCurrentCalendar];
    BOOL success = [gregrioan rangeOfUnit:NSMonthCalendarUnit startDate:&beginOfMonth interval:&endDate forDate:today];
    
    if (!success)
        NSLog(@"Error:failed to get day.");
    
    int day = ((int)endDate)/(3600*24);
    
    NSDateComponents *monthComponent = [gregrioan components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:today];
    
    NSDateComponents *component = [[NSDateComponents alloc]init];
    [component setDay:day];
    [component setMonth:[monthComponent month]];
    [component setYear:[monthComponent year]];
    
    
    NSDate *lastDate = [gregrioan dateFromComponents:component];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    
    NSString *strLastDate = [formatter stringFromDate:lastDate];
    
  
    
  //  NSString *strFirstDate = [formatter stringFromDate:beginOfMonth];
    
   // NSLog(@"strFirstDate====%@",strFirstDate);
    return strLastDate;
}

+(NSString *)floatConvertPercentile:(float)floatValue
{
   NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
   [numberFormatter setPositiveFormat:@"###,##0.00;"];
   NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:floatValue]];
    return formattedNumberString;
}
+(NSString *)getLoginToken
{
    
    return tokenStr;
}
+(void)setLoginToken:(NSString *)token
{
    if (tokenStr==nil) {
        tokenStr=[[NSMutableString alloc] init];
    }
    [tokenStr setString:token];
}

+(NSString *)getRongYunToken
{
    return rongYunTokenStr;
}
+(void)setRongYunToken:(NSString *)ryToken
{
    if (rongYunTokenStr==nil) {
        rongYunTokenStr=[[NSMutableString alloc] init];
    }
    [rongYunTokenStr setString:ryToken];
}

+(NSString *)getSelectedGoodType
{
    return selectedGoodTypeStr;
}
+(void)setSelectedGoodType:(NSString *)typeStr
{
    if (selectedGoodTypeStr==nil) {
        selectedGoodTypeStr=[[NSMutableString alloc] init];
    }
    [selectedGoodTypeStr setString:typeStr];
}

+(NSString *)getPhoneNumber
{
    return phoneNumStr;
}
+(void)setPhoneNumber:(NSString *)phoneNum
{
    if (phoneNumStr==nil) {
        phoneNumStr=[[NSMutableString alloc] init];
    }
    [phoneNumStr setString:phoneNum];
}

+(NSString *)getScanResultStr
{
    return scanResultStr;
}
+(void)setScanResultStr:(NSString *)resultString
{
    if (scanResultStr==nil) {
        scanResultStr=[[NSMutableString alloc] init];
    }
    [scanResultStr setString:resultString];
}
+(NSString *)getAccountCode
{
    return accountCodeStr;
}
+(void)setAccountCode:(NSString *)accountCode
{
    if (accountCodeStr==nil) {
        accountCodeStr=[[NSMutableString alloc] init];
    }
    [accountCodeStr setString:accountCode];
}

+(NSString *)getAccountName
{
    return accountNameStr;
}
+(void)setAccountName:(NSString *)accountName
{
    if (accountNameStr==nil) {
        accountNameStr=[[NSMutableString alloc] init];
    }
    [accountNameStr setString:accountName];
}

+(void)showTextHud:(NSString*)message view:(UIView *)view{
    _textHUD = [MBProgressHUD showHUDAddedTo:
                view animated:YES];
    [_textHUD setMode:MBProgressHUDModeText];
    [_textHUD setLabelText:message];
    [_textHUD setMargin:10.f];
    [_textHUD setYOffset:ScreenHeight/2- ScreenWidth/2-40];
    [_textHUD setXOffset:ScreenWidth/2-ScreenWidth/2+20];
    [_textHUD setRemoveFromSuperViewOnHide:YES];
    [_textHUD setUserInteractionEnabled:NO];
    [_textHUD hide:YES afterDelay:3];
}
+(void)showIndeterminateHud:(NSString*)text delay:(int)delay view:(UIView *)view{
    HUD = [MBProgressHUD showHUDAddedTo:
           view animated:YES];
    
    [HUD setMode:MBProgressHUDModeIndeterminate];
    [HUD setRemoveFromSuperViewOnHide:YES];
    [HUD setXOffset: ScreenWidth/2-ScreenWidth/2];
    [HUD hide:YES afterDelay:delay];
    [HUD setLabelText:text];
    [HUD setMargin:10.f];
   // NSLog(@"show hud:%@",HUD);
}
+(void)showTextHudSub:(NSString*)message view:(UIView *)view{
    _textHUD = [MBProgressHUD showHUDAddedTo:
                view animated:YES];
    [_textHUD setMode:MBProgressHUDModeText];
    [_textHUD setLabelText:message];
    [_textHUD setMargin:10.f];
    [_textHUD setYOffset:ScreenHeight/2- ScreenWidth/2-40];
   // [_textHUD setXOffset:240];
    [_textHUD setRemoveFromSuperViewOnHide:YES];
    [_textHUD setUserInteractionEnabled:NO];
    [_textHUD hide:YES afterDelay:3];
}

+(void)showIndeterminateHudSub:(NSString*)text delay:(int)delay view:(UIView *)view
{
    HUD = [MBProgressHUD showHUDAddedTo:
           view animated:YES];
    
    [HUD setMode:MBProgressHUDModeIndeterminate];
    [HUD setRemoveFromSuperViewOnHide:YES];
   // [HUD setXOffset:view.frame.size.width/2-200];
    [HUD hide:YES afterDelay:delay];
    [HUD setLabelText:text];
    [HUD setMargin:10.f];
}
+(void)hideProgressHUDInternal:(UIView *)view{
   
    MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
    if (hud != nil) {
   
        [hud removeFromSuperview];
        
     
    }
}

+(UIColor *)parseColor:(NSString *)hexColor {
    if(![[hexColor substringToIndex:1] isEqualToString:@"#"])
        return [UIColor clearColor];
    
    unsigned int alpha,red, green, blue;
    NSRange range = NSMakeRange(0, 2);
    NSString *scanStr = nil;
    
    if(hexColor.length==7){
        range.location = -1;
        alpha = 255;
    }else if(hexColor.length==9){
        range.location = 1;
        scanStr = [hexColor substringWithRange:range];
        [[NSScanner scannerWithString:scanStr]scanHexInt:&alpha];
    }
    
    range.location += 2;
    scanStr = [hexColor substringWithRange:range];
    [[NSScanner scannerWithString:scanStr]scanHexInt:&red];
    range.location += 2;
    scanStr = [hexColor substringWithRange:range];
    [[NSScanner scannerWithString:scanStr]scanHexInt:&green];
    range.location += 2;
    scanStr = [hexColor substringWithRange:range];
    [[NSScanner scannerWithString:scanStr]scanHexInt:&blue];
    
    return colorFromARGB(alpha, red, green, blue);
}

UIColor *colorFromARGB(CGFloat alpha,CGFloat red,CGFloat green,CGFloat blue){
    return [UIColor colorWithRed:red/255.0f green:green/255.0f
                            blue:blue/255.0f alpha:alpha/255.0f];
}

@end
